
package com.sun.showcase.biz.mall.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sun.showcase.biz.mall.dao.ShopDao;
import com.sun.showcase.biz.mall.service.ShopService;
import com.sun.showcase.client.domain.mall.Shop;
import com.sun.showcase.client.query.mall.ShopQuery;
import com.sun.showcase.pojo.DataGrid;
import com.sun.showcase.pojo.Pager;
import com.sun.showcase.utils.LoginContextHolder;
import com.sun.showcase.utils.StringConstant;
import com.sun.showcase.utils.UuidUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service("shopService")
@Transactional
public class ShopServiceImpl implements ShopService{
	@Autowired
	private ShopDao shopDao;
	
	public void setShopDao(ShopDao dao) {
		this.shopDao = dao;
	}

	public DataGrid datagrid(ShopQuery shopQuery) {
		DataGrid j = new DataGrid();
		Pager<Shop> pager  = find(shopQuery);
		j.setRows(getQuerysFromEntitys(pager.getRecords()));
		j.setTotal(pager.getTotalRecords());
		return j;
	}

	private List<ShopQuery> getQuerysFromEntitys(List<Shop> shops) {
		List<ShopQuery> shopQuerys = new ArrayList<ShopQuery>();
		if (shops != null && shops.size() > 0) {
			for (Shop tb : shops) {
				ShopQuery b = new ShopQuery();
				BeanUtils.copyProperties(tb, b);
				shopQuerys.add(b);
			}
		}
		return shopQuerys;
	}

	private Pager<Shop> find(ShopQuery shopQuery) {
		Page<Shop> page = PageHelper.startPage(shopQuery.getPage().intValue(), shopQuery.getRows().intValue());
		List<Shop> list = shopDao.findList(shopQuery);
		Pager<Shop> pager = new Pager<Shop>();
		pager.setRecords(list);
		pager.setTotalRecords(page.getTotal());
		return  pager;
		
	}
	


	public Shop add(ShopQuery shopQuery) {
		Shop t = new Shop();
		BeanUtils.copyProperties(shopQuery, t);
		t.setActiveFlag(StringConstant.ONE);
		t.setCreateBy(LoginContextHolder.get().getUserId());
		t.setCreateByName(LoginContextHolder.get().getUserName());
		t.setCreateDate(new Date());
	    t.setModifiedBy(LoginContextHolder.get().getUserId());
	    t.setModifiedByName(LoginContextHolder.get().getUserName());
	    t.setModifiedDate(new Date());
	    t.setId(UuidUtil.get32UUID());
		shopDao.save(t);
		shopQuery.setId(t.getId());
		return t;
		
	}

	public void update(ShopQuery shopQuery) {
		Shop t = shopDao.getById(shopQuery.getId());
	    if(t != null) {
	    	BeanUtils.copyProperties(shopQuery, t);
		    t.setModifiedBy(LoginContextHolder.get().getUserId());
		    t.setModifiedByName(LoginContextHolder.get().getUserName());
		    t.setModifiedDate(new Date());
	    	shopDao.update(t);
		}
	}
	
	/**
	 * 根据条件更新部分字段   新建一个query封装需要更新的字段
	 * searchMap封装更新条件
	 * */
	public void updatePart(ShopQuery shopQuery) {
	    shopDao.updatePart(shopQuery);
	}

	public void delete(String[] ids) {
		shopDao.deleteAc(ids);
	}

	//逻辑删除 更新状态位
	public void deletePt(String[] ids) {
		shopDao.deletePt(ids);
	}

	public Shop get(ShopQuery shopQuery) {
		return shopDao.getById(shopQuery.getId());
	}

	public Shop get(String id) {
		return shopDao.getById(id);
	}

	
	public List<ShopQuery> listAll(ShopQuery shopQuery) {
	    List<Shop> list = shopDao.findList(shopQuery);
		List<ShopQuery> listQuery =getQuerysFromEntitys(list) ;
		return listQuery;
	}
	
	
}
